﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterAdmin.master" AutoEventWireup="true"
    CodeBehind="OrderMaterial.aspx.cs" Inherits="UdrinkWater_Project.Admin.OrderMaterial" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
    // <![CDATA[
        function ShowSertEmpWindow() {
            pcDataEmp.Show();
        }
        function ShowSertCusWindow() {
            pcCusData.Show();
        }
        function ShowSertProWindow() {
            pcMaterial.Show();
        }
    // ]]> 
    </script>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <dx:ASPxPopupControl ID="pcMaterial" runat="server" HeaderText="ค้นหาวัตถุดิบ" 
        ClientInstanceName="pcMaterial" EnableViewState="False" 
        PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter" 
        Width="900px">
        <ContentCollection>
<dx:PopupControlContentControl runat="server" SupportsDisabledAttribute="True">
    <dx:ASPxGridView ID="ASPxGridView3" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataItem" KeyFieldName="Material_ID" 
        OnRowCommand="ASPxGridView4_RowCommand" Width="100%">
        <Columns>
            <dx:GridViewDataTextColumn Caption="รหัสวัตถุดิบ" FieldName="Material_ID" 
                ShowInCustomizationForm="True" VisibleIndex="1">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="ชื่อวัตถุดิบ" FieldName="Materail_name" 
                ShowInCustomizationForm="True" VisibleIndex="2">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="จำนวนคงเหลือ" FieldName="TotalSum" 
                ReadOnly="True" ShowInCustomizationForm="True" VisibleIndex="3">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="หน่วยนับ" FieldName="Unit_Name" 
                ShowInCustomizationForm="True" VisibleIndex="4">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="เลือก" ShowInCustomizationForm="True" 
                VisibleIndex="5">
                <DataItemTemplate>
                    <dx:ASPxButton ID="btSertProd" runat="server" 
                        CommandArgument='<%# Eval("Material_ID") %>' CommandName="Cmd_Select" 
                        Text="เลือก">
                        <ClientSideEvents Click="function(s, e) {
	pcMaterial.Hide();
}" />
                    </dx:ASPxButton>
                </DataItemTemplate>
            </dx:GridViewDataTextColumn>
        </Columns>
        <Settings ShowFilterRow="True" />
    </dx:ASPxGridView>
    <asp:SqlDataSource ID="SqlDataItem" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_Udrink %>" 
        
        SelectCommand="SELECT seUnit.Material_ID, seUnit.Materail_name, ISNULL(Inm.Total - OutM.SumTotal, 0) AS TotalSum, seUnit.Unit_Name FROM (SELECT Material_ID, ISNULL(SUM(Material_Count), 0) AS Total FROM TB_Material_StockIN GROUP BY Material_ID) AS Inm FULL OUTER JOIN (SELECT Material_ID, ISNULL(SUM(Material_Count), 0) AS SumTotal FROM TB_Material_StockOUT GROUP BY Material_ID) AS OutM ON Inm.Material_ID = OutM.Material_ID INNER JOIN (SELECT Mat.Material_ID, Mat.Materail_name, Unit.Unit_Name FROM TB_Material AS Mat INNER JOIN TB_Unit_Category AS Unit ON Mat.Unit = Unit.Unit_ID) AS seUnit ON Inm.Material_ID = seUnit.Material_ID"></asp:SqlDataSource>

            </dx:PopupControlContentControl>
</ContentCollection>
    </dx:ASPxPopupControl>
    <dx:ASPxRoundPanel ID="ASPxRoundPanel1" runat="server" Width="100%" HeaderText="รายการสั่งซื้ออุปกรณ์">
        <PanelCollection>
            <dx:PanelContent runat="server" SupportsDisabledAttribute="True">
                <asp:Panel ID="PanelMaster" runat="server" Width="100%" BorderStyle="None">
                </asp:Panel>
                <asp:Panel ID="PanelDetail" runat="server" Width="100%">
                    <table style="width: 100%">
                        <tr>
                            <td style="height: inherit; width: 40%;" valign="top">
                                <asp:Panel ID="Panel2" runat="server">
                                    <fieldset>
                                        <legend>วันที่เวลา</legend>
                                        <asp:UpdatePanel ID="updDateOrder" runat="server">
                                            <ContentTemplate>
                                                <table style="width: 100%">
                                                    <tr>
                                                        <td style="width: 78px">
                                                            <dx:ASPxLabel ID="ASPxLabel3" runat="server" Text="วันเวลาเอกสาร:">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td>
                                                            <dx:ASPxTextBox ID="txtOrderDate" runat="server" BackColor="#D3E4A6" ReadOnly="True"
                                                                Width="225px">
                                                            </dx:ASPxTextBox>
                                                        </td>
                                                        <td>
                                                            &nbsp;
                                                        </td>
                                                    </tr>
                                                </table>
                                            </ContentTemplate>
                                        </asp:UpdatePanel>
                                    </fieldset>
                                    <asp:Panel ID="Panel1" runat="server">
                                        <asp:Panel ID="Panel3" runat="server">
                                            <fieldset>
                                                <legend>ที่อยู่การจัดซื้อ</legend>
                                                <asp:UpdatePanel ID="updCustomer" runat="server">
                                                    <ContentTemplate>
                                                        <table style="width: 100%">
                                                            <tr>
                                                                <td style="width: 90px;">
                                                                    <dx:ASPxLabel ID="ASPxLabel6" runat="server" Text="ที่อยู่:">
                                                                    </dx:ASPxLabel>
                                                                </td>
                                                                <td>
                                                                    <dx:ASPxTextBox ID="txtAdd" runat="server" Width="170px">
                                                                    </dx:ASPxTextBox>
                                                                </td>
                                                                <td>
                                                                    &nbsp;</td>
                                                            </tr>
                                                            <tr>
                                                                <td style="width: 90px; height: 18px">
                                                                    <dx:ASPxLabel ID="ASPxLabel19" runat="server" Text="จังหวัด:">
                                                                    </dx:ASPxLabel>
                                                                </td>
                                                                <td style="height: 18px">
                                                                    <asp:DropDownList ID="dlPro" runat="server" AutoPostBack="True" 
                                                                        DataSourceID="SqlProvince" DataTextField="PROVINCE_NAME" 
                                                                        DataValueField="PROVINCE_ID" Width="170px">
                                                                    </asp:DropDownList>
                                                                    <asp:SqlDataSource ID="SqlProvince" runat="server" 
                                                                        ConnectionString="<%$ ConnectionStrings:DB_Udrink %>" 
                                                                        SelectCommand="SELECT TOP (1000) PROVINCE_ID, PROVINCE_CODE, PROVINCE_NAME FROM TB_Province ORDER BY PROVINCE_NAME">
                                                                    </asp:SqlDataSource>
                                                                </td>
                                                                <td style="height: 18px">
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td style="width: 90px">
                                                                    <dx:ASPxLabel ID="ASPxLabel20" runat="server" Text="อำเภอ:">
                                                                    </dx:ASPxLabel>
                                                                </td>
                                                                <td>
                                                                    <asp:DropDownList ID="dlAmp" runat="server" AutoPostBack="True" 
                                                                        DataSourceID="SqlAumphur" DataTextField="AMPHUR_NAME" 
                                                                        DataValueField="AMPHUR_ID" Width="170px">
                                                                    </asp:DropDownList>
                                                                    <asp:SqlDataSource ID="SqlAumphur" runat="server" 
                                                                        ConnectionString="<%$ ConnectionStrings:DB_Udrink %>" 
                                                                        SelectCommand="SELECT AMPHUR_ID, AMPHUR_CODE, AMPHUR_NAME, PROVINCE_ID FROM TB_Aumphur WHERE (PROVINCE_ID = @PROVINCE_ID)">
                                                                        <SelectParameters>
                                                                            <asp:ControlParameter ControlID="dlPro" Name="PROVINCE_ID" 
                                                                                PropertyName="SelectedValue" Type="Int32" />
                                                                        </SelectParameters>
                                                                    </asp:SqlDataSource>
                                                                </td>
                                                                <td>
                                                                    &nbsp;</td>
                                                            </tr>
                                                            <tr>
                                                                <td style="width: 90px">
                                                                    <dx:ASPxLabel ID="ASPxLabel21" runat="server" Text="ตำบล:">
                                                                    </dx:ASPxLabel>
                                                                </td>
                                                                <td>
                                                                    <asp:DropDownList ID="dlDis" runat="server" AutoPostBack="True" 
                                                                        DataSourceID="SqlDistrict" DataTextField="DISTRICT_NAME" 
                                                                        DataValueField="DISTRICT_ID" Width="170px">
                                                                    </asp:DropDownList>
                                                                    <asp:SqlDataSource ID="SqlDistrict" runat="server" 
                                                                        ConnectionString="<%$ ConnectionStrings:DB_Udrink %>" 
                                                                        SelectCommand="SELECT [DISTRICT_ID], [DISTRICT_CODE], [DISTRICT_NAME], [AMPHUR_ID], [PROVINCE_ID] FROM [TB_District] WHERE (([PROVINCE_ID] = @PROVINCE_ID) AND ([AMPHUR_ID] = @AMPHUR_ID))">
                                                                        <SelectParameters>
                                                                            <asp:ControlParameter ControlID="dlPro" Name="PROVINCE_ID" 
                                                                                PropertyName="SelectedValue" Type="Int32" />
                                                                            <asp:ControlParameter ControlID="dlAmp" Name="AMPHUR_ID" 
                                                                                PropertyName="SelectedValue" Type="Int32" />
                                                                        </SelectParameters>
                                                                    </asp:SqlDataSource>
                                                                </td>
                                                                <td>
                                                                    &nbsp;</td>
                                                            </tr>
                                                            <tr>
                                                                <td style="width: 90px">
                                                                    &nbsp;</td>
                                                                <td>
                                                                    &nbsp;</td>
                                                                <td>
                                                                    &nbsp;</td>
                                                            </tr>
                                                        </table>
                                                    </ContentTemplate>
                                                </asp:UpdatePanel>
                                             
                                            </fieldset>
                                        </asp:Panel>
                                    </asp:Panel>
                                </asp:Panel>
                            </td>
                            <td style="height: inherit" valign="top" width="40%">
                                <asp:Panel ID="Panel4" runat="server" Height="100%">
                                    <fieldset>
                                        <legend>เพิ่มสินค้าในรายการ</legend>
                                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                            <ContentTemplate>
                                                <table style="width: 100%">
                                                    <tr>
                                                        <td style="width: 78px">
                                                            <dx:ASPxLabel ID="ASPxLabel7" runat="server" Text="รหัสสินค้า:">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td>
                                                            <dx:ASPxTextBox ID="txtProductId" runat="server" BackColor="#D3E4A6" ReadOnly="True"
                                                                Width="225px">
                                                            </dx:ASPxTextBox>
                                                            <dx:ASPxButton ID="btProductSert" runat="server" AutoPostBack="False" 
                                                                Text="ค้นหา" Width="60px">
                                                                <ClientSideEvents Click="function(s, e) {
	ShowSertProWindow();
}" />
                                                            </dx:ASPxButton>
                                                        </td>
                                                        <td>
                                                            &nbsp;</td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 78px">
                                                            <dx:ASPxLabel ID="ASPxLabel8" runat="server" Text="ชื่อสินค้า:">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td>
                                                            <dx:ASPxTextBox ID="txtProductName" runat="server" BackColor="#D3E4A6" ReadOnly="True"
                                                                Width="225px">
                                                            </dx:ASPxTextBox>
                                                        </td>
                                                        <td>
                                                            &nbsp;
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 78px; height: 25px;">
                                                            <dx:ASPxLabel ID="ASPxLabel10" runat="server" Text="จำนวนคงเหลือ:">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td style="height: 25px">
                                                            <dx:ASPxTextBox ID="txtInStock" runat="server" BackColor="#D3E4A6" ReadOnly="True"
                                                                Width="180px">
                                                            </dx:ASPxTextBox>
                                                        </td>
                                                        <td rowspan="6">
                                                            &nbsp;
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 78px">
                                                            <dx:ASPxLabel ID="ASPxLabel12" runat="server" Text="หน่วยสินค้า:">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td>
                                                            <dx:ASPxTextBox ID="txtUnit" runat="server" BackColor="#D3E4A6" ReadOnly="True" Width="180px">
                                                            </dx:ASPxTextBox>
                                                            <asp:HiddenField ID="hdUnit" runat="server" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="height: 23px; width: 78px;">
                                                            <dx:ASPxLabel ID="ASPxLabel9" runat="server" Text="ราคา/หน่วย:">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td style="height: 23px">
                                                            <dx:ASPxTextBox ID="txtUnitPrice" runat="server" Width="180px">
                                                            </dx:ASPxTextBox>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 78px; height: 25px;">
                                                            <dx:ASPxLabel ID="ASPxLabel11" runat="server" Text="จำนวนซื้อ:">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td style="height: 25px">
                                                            <dx:ASPxTextBox ID="txtCount" runat="server" ClientInstanceName="txtCount" Style="margin-bottom: 0px"
                                                                Width="180px">
                                                            </dx:ASPxTextBox>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 78px">
                                                            &nbsp;
                                                        </td>
                                                        <td>
                                                            &nbsp;
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 78px">
                                                            &nbsp;
                                                        </td>
                                                        <td align="center">
                                                            <dx:ASPxLabel ID="lbWranList" runat="server" ForeColor="Red" Text="สินค้ามีในรายการแล้ว!"
                                                                Visible="False">
                                                            </dx:ASPxLabel>
                                                        </td>
                                                        <td>
                                                            &nbsp;
                                                        </td>
                                                    </tr>
                                                </table>
                                            </ContentTemplate>
                                        </asp:UpdatePanel>
                                        <table style="width: 100%">
                                            <tr>
                                                <td align="center" style="height: 27px">
                                                    <dx:ASPxButton ID="btAddList" runat="server" Text="เพิ่มรายการ" 
                                                        OnClick="btAddList_Click" AutoPostBack="False">
                                                    </dx:ASPxButton>
                                                </td>
                                            </tr>
                                        </table>
                                    </fieldset>
                                </asp:Panel>
                            </td>
                            <td style="height: inherit" valign="top" width="20%">
                                <asp:Panel ID="Panel5" runat="server">
                                    <table style="width: 100%">
                                        <tr>
                                            <td align="center">
                                                <dx:ASPxButton ID="btSaveAndReport" runat="server" Height="50px" 
                                                    OnClick="btSaveAndReport_Click" Text="บันทึกและพิมพ์ใบเสร็จ" Width="180px">
                                                </dx:ASPxButton>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="center">
                                                <dx:ASPxButton ID="btNewOrder" runat="server" Height="50px" 
                                                    OnClick="btNewOrder_Click" Text="เริ่มรายการใหม่" Width="180px">
                                                </dx:ASPxButton>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td align="center">
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                &nbsp;
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="center">
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td align="center">
                                                &nbsp;</td>
                                        </tr>
                                    </table>
                                </asp:Panel>
                            </td>
                        </tr>
                    </table>
                    <asp:Panel ID="Panel6" runat="server">
                        <dx:ASPxGridView ID="ASPxdtail" runat="server" AutoGenerateColumns="False" 
                            Width="100%" OnRowCommand="ASPxdtail_RowCommand" 
                            KeyFieldName="Material_ID">
                            <Columns>
                                <dx:GridViewDataComboBoxColumn Caption="รายการ" FieldName="Material_ID" 
                                    ShowInCustomizationForm="True" VisibleIndex="0">
                                    <PropertiesComboBox DataSourceID="Sqldtail" TextField="Materail_name" 
                                        ValueField="Material_ID">
                                    </PropertiesComboBox>
                                </dx:GridViewDataComboBoxColumn>
                                <dx:GridViewDataTextColumn Caption="จำนวน" ShowInCustomizationForm="True" 
                                    VisibleIndex="1" FieldName="Material_Count">
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataComboBoxColumn Caption="หน่วยสินค้า" FieldName="Unit" 
                                    ShowInCustomizationForm="True" VisibleIndex="2">
                                    <PropertiesComboBox DataSourceID="SqlUnit" TextField="Unit_Name" 
                                        ValueField="Unit_ID">
                                    </PropertiesComboBox>
                                </dx:GridViewDataComboBoxColumn>
                                <dx:GridViewDataTextColumn Caption="ราคา / หน่วย" 
                                    ShowInCustomizationForm="True" VisibleIndex="3" FieldName="Price">
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn Caption="จำนวนเงิน" ShowInCustomizationForm="True"
                                    VisibleIndex="4" FieldName="Total">
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn Caption="ลบ" ShowInCustomizationForm="True" VisibleIndex="5">
                                    <DataItemTemplate>
                                        <dx:ASPxButton ID="btnImageAndText" runat="server" BackColor="White" 
                                                        CommandArgument='<%#Eval("Material_ID")%>' CommandName="Cmd_Delete" 
                                                        Text="ลบรายการ" Width="120px">
                                                        <ClientSideEvents Click="function(s,e) { e.processOnServer = confirm('Are you sure?'); }" />
                                                    </dx:ASPxButton>
                                    </DataItemTemplate>
                                </dx:GridViewDataTextColumn>
                            </Columns>
                        </dx:ASPxGridView>
                        <asp:SqlDataSource ID="Sqldtail" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:DB_Udrink %>" 
                            SelectCommand="SELECT Material_ID, Materail_name FROM TB_Material">
                        </asp:SqlDataSource>
                        <asp:SqlDataSource ID="SqlMaterialStatus" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:DB_Udrink %>" 
                            SelectCommand="SELECT [M_Status_ID], [M_Status_Name] FROM [TB_MaterialStatus]">
                        </asp:SqlDataSource>
                        <asp:SqlDataSource ID="SqlUnit" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:DB_Udrink %>" 
                            SelectCommand="SELECT Unit_ID, Unit_Name FROM TB_Unit_Category">
                        </asp:SqlDataSource>
                        <asp:HiddenField ID="chkRow" runat="server" />
                    </asp:Panel>
                </asp:Panel>
            </dx:PanelContent>
        </PanelCollection>
    </dx:ASPxRoundPanel>
</asp:Content>
